/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spaceshooter;

import it.randomtower.engine.ME;
import it.randomtower.engine.ResourceManager;
import it.randomtower.engine.entity.Entity;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;

/**
 *
 * @author Bensch
 */
public class Ship extends Entity {
    
    private Image player, playerRight, playerLeft;
    public static String SHIP = "ship";
    private int fireRate = 50;
    private int milliCount = 0;
    private int millis  = fireRate;
    private int milliStep = millis / 5;
    
    public Ship(float x, float y) {
        super(x,y);
        player = ResourceManager.getImage("player");
        playerRight = ResourceManager.getImage("playerRight");
        playerLeft = ResourceManager.getImage("playerLeft");
        setGraphic(player);
        setHitBox(2, 6, 59, 52);
        addType(SHIP);
        define("right", Input.KEY_RIGHT, Input.KEY_D);
        define("left", Input.KEY_LEFT, Input.KEY_A);
        define("up", Input.KEY_UP, Input.KEY_W);
        define("down", Input.KEY_DOWN, Input.KEY_S);
    }
    
    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        super.update(gc, delta);
        
        Input input = gc.getInput();
        MouseListener ml = new MouseListener() {

            @Override
            public void mouseWheelMoved(int i) {
               
            }

            @Override
            public void mouseClicked(int i, int i1, int i2, int i3) {
                
            }

            @Override
            public void mousePressed(int i, int i1, int i2) {
                
            }

            @Override
            public void mouseReleased(int i, int i1, int i2) {
               
            }

            @Override
            public void mouseMoved(int i, int i1, int i2, int i3) {
                
            }

            @Override
            public void mouseDragged(int i, int i1, int i2, int i3) {
               
            }

            @Override
            public void setInput(Input input) {
                
            }

            @Override
            public boolean isAcceptingInput() {
                return true;
            }

            @Override
            public void inputEnded() {
                
            }

            @Override
            public void inputStarted() {
                
            }
        };
        
        input.addMouseListener(ml);
             
        if(check("right") && x + player.getWidth() < world.width) {
            setGraphic(playerRight);
            x += (.4 * delta);
        }
        
        if(check("left") && x > 0) {
            setGraphic(playerLeft);
            x -= (.4 * delta);
        }
        
        if(check("up") && y > 0) {
            setGraphic(player);
            y -= (.4 * delta);
        }
       
        if(check("down") && y + player.getHeight() < world.height) {
            setGraphic(player);
            y += (.4 * delta);
        }
        
        if(!check("right") && !check("left")) {
            setGraphic(player);
        }
        
        if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            milliCount += delta;
            while(milliCount > milliStep) {
                milliCount -= milliStep;
                millis -= milliStep;
            }
            if(millis <= 0) {
                Bullet b = new Bullet(x + 32, y);
                ME.world.add(b);
                millis = fireRate;
            }
        }        
    }
    
}
